﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _6
{
    public partial class Form1 : Form
    {
        private string file1 = "";
        private string file2 = "";
        public Form1()
        {
            InitializeComponent();
        }

        //按钮1
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        openFileDialog.Filter = "文本文件 (*.txt)|*.txt";
                        file1 = openFileDialog.FileName;
                        label1.Text = $@"已选择{Path.GetFileName(file1)}";
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("文件错误，请重新选择");
            }
        }

        //按钮2
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        openFileDialog.Filter = "文本文件 (*.txt)|*.txt";
                        file2 = openFileDialog.FileName;
                        label2.Text = $@"已选择{Path.GetFileName(file2)}";
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("文件错误，请重新选择");
            }
        }

        //合并按钮
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                //确认已经选择了两个文件
                if (!string.IsNullOrEmpty(file1) && !string.IsNullOrEmpty(file2))
                {
                    // 检查两文件是否为文本文件
                    if (Path.GetExtension(file1).Equals(".txt", StringComparison.OrdinalIgnoreCase) &&
                        Path.GetExtension(file2).Equals(".txt", StringComparison.OrdinalIgnoreCase))
                    {
                        string mergedFilePath = Path.Combine(Path.GetDirectoryName(file1), "MergedFile.txt");

                        // Read contents of both files
                        string file1Content = File.ReadAllText(file1);
                        string file2Content = File.ReadAllText(file2);

                        // Merge contents
                        string mergedContent = $"{file1Content}{Environment.NewLine}{file2Content}";

                        // Write merged content to new file
                        File.WriteAllText(mergedFilePath, mergedContent);
                        MessageBox.Show("合并成功");
                        Process.Start("explorer.exe", $"/select,\"{mergedFilePath}\"");
                    }
                    else
                    {
                        MessageBox.Show("请选择两个文本文件");
                    }
                }
                else
                {
                    MessageBox.Show("请选择文件");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("错误");
            }
        }
    }
}